package com.cdel.akka.agent;

import scala.concurrent.Future;
import akka.actor.UntypedActor;
import akka.dispatch.Mapper;

public class CounterActor extends UntypedActor {

	Mapper<Integer, Integer> addMapper = new Mapper<Integer, Integer>() {
		@Override
		public Integer apply(Integer i){
			return i+1;
		}
	};

	@Override
	//对全局的counterAgent进行累加操作，alter指定了累加动作addMappper。由于我们希望在将来知道累加行为是否完成，因此在这里返回的future对象进行收集。任务完成后，actor自行退出
	public void onReceive(Object msg) throws Throwable {
		if(msg instanceof Integer){
			for(int i=0; i<10000; i++){
				//我们希望知道future何时结束
				Future<Integer> f = AgentDemo.counterAgent.alter(addMapper);
				AgentDemo.futures.add(f);
			}
			getContext().stop(getSelf());
		}else{
			unhandled(msg);
		}
	}

}
